package cur.nature.starter.common.config;

import cur.nature.framework.core.log.LOG;
import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {
        "mybatis.manager.mapper",
//        "com.cur.main.base.mapper",
//        "com.cur.main.cloud.mapper",
//        "com.cur.main.platform.*.mapper"
}, sqlSessionTemplateRef  = "curAdminSessionTemplate")
//@ConditionalOnProperty( value = {"cur.admin.manager.enable"})
public class CurAdminDBConfig {
//	static final String MAPPER_LOCATIONS = "classpath*:net/hkt/xml/*.xml,classpath:mapper_hkt/*.xml";
	static final String MAPPER_LOCATIONS = "classpath*:mybatis/manager/mapper/*.xml";
    public final static String TEMPLATE = "curAdminTemplate";

//    @RefreshScope
	@Bean(name = DefaultJdbcTemplate.DATASOURCE)
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "curAdminSessionFactory")
    public SqlSessionFactory curAdminSessionFactory(@Qualifier(DefaultJdbcTemplate.DATASOURCE) DataSource dataSource) throws Exception {
        LOG.info("开始初始化数据连接池。。。。。。。");
    	final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATIONS));
//        String[] locations = MAPPER_LOCATIONS.split(",");
//        PathMatchingResourcePatternResolver matches = new PathMatchingResourcePatternResolver();
//        Resource[] res = matches.getResources(locations[0].trim());
//        Resource[] res2 = matches.getResources(locations[1].trim());
//        sessionFactory.setMapperLocations((Resource[])ArrayUtils.addAll(res, res2));
//        return sessionFactory.getObject();

        // 分页插件
//        PageHelper pageHelper = new PageHelper();
//        Properties props = new Properties();
//        props.setProperty("reasonable", "true");
//        props.setProperty("supportMethodsArguments", "true");
//        props.setProperty("returnPageInfo", "check");
//        props.setProperty("params", "count=countSql");
//        pageHelper.setProperties(props);
//        // 添加插件
//        sessionFactory.setPlugins(new Interceptor[] { pageHelper });
        //设置类型转换器
//        sessionFactory.setTypeHandlers();
        LOG.info("结束初始化数据连接池。。。。。。。");
        return sessionFactory.getObject();
    }

    @Bean(name = "curAdminTransactionManager")
    public DataSourceTransactionManager curAdminTransactionManager(@Qualifier(DefaultJdbcTemplate.DATASOURCE) DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "curAdminSessionTemplate")
    public SqlSessionTemplate curAdminSessionTemplate(@Qualifier("curAdminSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = DefaultJdbcTemplate.TEMPLATE)
    public DefaultJdbcTemplate curAdminTemplate(@Qualifier(DefaultJdbcTemplate.DATASOURCE) DataSource dataSource) {
        return new DefaultJdbcTemplate(dataSource);
    }
}